package com.huizu.pay.util.pay.AliPay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.huizu.pay.util.AjaxResult;
import com.huizu.pay.util.PayConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class AliPay {

    public static AjaxResult doPay(String orderId, String orderDesc, String oderName, String actualPrice, String aliPayNotifyUrl) throws AlipayApiException {
        AjaxResult result = new AjaxResult();
        AlipayClient alipayClient = new DefaultAlipayClient(PayConstants.ALI_PAY_GATEWAY_URL, PayConstants.ALI_PAY_APP_ID, PayConstants.ALI_PAY_MERCHANT_PRIVATE_KEY, PayConstants.PAY_JSON, PayConstants.PAY_CHARSET, PayConstants.ALI_PAY_PUBLIC_KEY,PayConstants.ALI_PAY_SIGN_TYPE);
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        /*"商品详情"*/
        model.setBody(orderDesc);
        /*关键字 标题*/
        model.setSubject(oderName);
        /*订单id*/
        model.setOutTradeNo(orderId);
        /*过期时间*/
        model.setTimeoutExpress("15m");
        /*价格*/
        model.setTotalAmount(String.format("%.2f", Double.valueOf(actualPrice)));
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl(aliPayNotifyUrl);
            AlipayTradeAppPayResponse payResponse = alipayClient.sdkExecute(request);
            if (payResponse.isSuccess()) {
                String body = payResponse.getBody();
                System.out.println("aliBody:" + body);
                result.put("code", "0");
                result.put("msg", "调用支付成功");
                result.put("data", body);
            } else {
                result.put("code", "1");
                result.put("msg", "调用支付失败");
            }
            return result;
    }

    /**
     * 校验签名 并将返回所有数据存入map返回
     * @param request
     * @return
     * @throws AlipayApiException
     */
    public static Map<String,String> rsaCheckV1AndGetInfo(HttpServletRequest request) throws AlipayApiException {
        //获取支付宝POST过来反馈信息
        Map<String, String> params = new HashMap<>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
//        boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
        if (AlipaySignature.rsaCheckV1(params, PayConstants.ALI_PAY_PUBLIC_KEY, PayConstants.PAY_CHARSET, PayConstants.ALI_PAY_SIGN_TYPE)) {
            return params;
        }
        return new HashMap<>(1);
    }

}
